package com.klikmee.sms.core.base.creators;

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashSet;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.Core;
import com.klikmee.sms.core.base.dao.OutgoingMessageFacadeLocal;
import com.klikmee.sms.core.base.dao.PaymentInvoiceFacadeLocal;
import com.klikmee.sms.core.base.util.Constants;
import com.klikmee.sms.core.base.util.States;
import com.klikmee.sms.core.model.IncomingMessage;
import com.klikmee.sms.core.model.OutgoingMessage;
import com.klikmee.sms.core.model.PaymentInvoice;
import com.klikmee.sms.core.model.ServiceChargingCategory;

/**
 * <p>
 * Default implementation of {@link MessageCreator} interface.
 * Creates charged messages.
 * </p>
 * 
 */
public class SimpleMessageCreatorImpl implements MessageCreator {
	
	/**
	 * Logger instance
	 */
	private Logger logger = Logger.getLogger(SimpleMessageCreatorImpl.class);
	
	/**
	 * Ref to the core component used to enqueue new messages for sending
	 */
	private Core						core;
	/**
	 * Outgoing message dao, used to create messages
	 */
	private OutgoingMessageFacadeLocal	outgoingMessageDAO;
	/**
	 * Payment invoice dao used to create invoices
	 */
	private PaymentInvoiceFacadeLocal	paymentInvoiceDAO;

	public SimpleMessageCreatorImpl() {
	}

	public void setOutgoingMessageDAO( OutgoingMessageFacadeLocal outgoingMessageDAO ) {
		this.outgoingMessageDAO = outgoingMessageDAO;
	}

	public void setPaymentInvoiceDAO( PaymentInvoiceFacadeLocal paymentInvoiceDAO ) {
		this.paymentInvoiceDAO = paymentInvoiceDAO;
	}

	public void setCore( Core core ) {
		this.core = core;
	}

	public void init() {
		logger.info("Simple message creator initialized.");
	}

	/**
	 * <p>
	 * Creates response message for customers request. 
	 * Request and response message are bind with newly created {@link PaymentInvoice} instance with specified charging. 
	 * </p>
	 * <p>
	 * After messages is stored in database,
	 * it's enqueued for sending.
	 * </p>
	 * @param incomingMessage customers request message
	 * @param charging charging used for this response
	 * @param responseText text of the response message
	 * @return core id of the created response message
	 */
	public Long createResponse( IncomingMessage incomingMessage, ServiceChargingCategory charging, String responseText ) {

		// create response message
		OutgoingMessage outgoingMessage = OutgoingMessage.createFrom( incomingMessage );
		outgoingMessage.setService( charging.getService() );
		outgoingMessage.setMessage( responseText );
		outgoingMessage.setState( States.OutgoingMessage.WAITING_TO_BE_SENT );
		outgoingMessage.setTypeOfMessage( Constants.MESSAGE_TYPE_SMS );
		
		

		// create payment invoice
		PaymentInvoice paymentInvoice = new PaymentInvoice();

		paymentInvoice.setCreated( new Date() );
		paymentInvoice.setIncomingMessage( incomingMessage );
		paymentInvoice.setOutgoingMessage( outgoingMessage );
		paymentInvoice.setServiceChargingCategory( charging );
		paymentInvoice.setState( States.PaymentInvoice.WAITING_TO_BE_CHARGED );
		paymentInvoice.setType( Constants.PI_TYPE_CHARGED_MESSAGE );
		
		// add payment invoice to outgoing message
		outgoingMessage.setPaymentInvoiceList( new  HashSet<PaymentInvoice>());
		outgoingMessage.getPaymentInvoiceList().add( paymentInvoice );
		// create outgoing message
		outgoingMessageDAO.create( outgoingMessage );
		
		logger.info(MessageFormat.format("Created outgoing message with id = [{0}] and payment invoice with id = [{1}]", outgoingMessage.getId(), paymentInvoice.getId()));

		// enqueue message for sending
		core.enqueueOutgoingMessage( outgoingMessage );
		
		return outgoingMessage.getId();
	}
	
}
